package tt

import (
	"sync"
	"time"
)

var done = false

func read(name string, c *sync.Cond) {
//func read(name string, c *sync.Mutex) {
//	c.Lock()
	c.L.Lock()
	println("ldd")
	for !done {
		println("a")
		c.Wait()
		println("c")
	}
	//log.Println(name, "starts reading")
	c.L.Unlock()
	//c.Unlock()
}

func write(name string, c *sync.Cond) {
	//func read(name string, c *sync.Mutex) {
	//log.Println(name, "starts writing")
	time.Sleep(time.Second)
	c.L.Lock()
	//c.Lock()
	println("b")
	done = true
	println("t")
	c.L.Unlock()

	println("g")
	//c.Unlock()
	//log.Println(name, "wakes all")
	//c.Broadcast()
}

func DoIt() {
	cond := sync.NewCond(&sync.Mutex{})
	go read("reader1", cond)
	go read("reader2", cond)
	go read("reader3", cond)
	write("writer", cond)

	go read("reader3", cond)
	//lock := &sync.Mutex{}
	//go read("reader1", lock)
	//go read("reader2", lock)
	//go read("reader3", lock)
	//write("writer", lock)

	time.Sleep(time.Second * 3)
}